package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 创建日期: 2022/05/19 09:53
 *
 * @author shuyang
 */
@Slf4j
@RestController
public class ShoppingCartsController {
    @Autowired
    ICartService iCartService;
    //获取购物车
    @GetMapping("shopping/carts")
    public ResponseData getShoppingCarts(HttpServletRequest request) {

        String userInfo = request.getHeader(GlobalConstants.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get(GlobalConstants.USER_INFO_ID_KEY).toString());
<<<<<<< HEAD
=======

        // String userInfo = request.getHeader(GlobalConstants.USER_INFO_KEY);
        // long uid = Long.parseLong(userInfo);

>>>>>>> 1864a7f3ee87996cfdb2da904951f8e527f711eb
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse cartListById = iCartService.getCartListById(cartListByIdRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(cartListById.getCode())) {
            return new ResponseUtil().setData(cartListById.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(cartListById.getMsg());
    }
    //添加购物车
    @PostMapping("shopping/carts")
    public ResponseData addItemToShoppingCarts(@RequestBody CartForm cartForm) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(cartForm.getUserId());
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());
        AddCartResponse addCartResponse = iCartService.addToCart(addCartRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }
        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }
    //改变购物车商品数量和是否被选中
    @PutMapping("shopping/carts")
    public ResponseData updateCartNum(@RequestBody CartForm cartForm) {
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(cartForm.getUserId());
        updateCartNumRequest.setItemId(cartForm.getProductId());
        updateCartNumRequest.setChecked(cartForm.getChecked());
        updateCartNumRequest.setNum(cartForm.getProductNum());
        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(updateCartNumRequest);
        if ((ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode()))) {
            return new ResponseUtil().setData(updateCartNumResponse);
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }
    //删除购物车的商品
    //在界面购物车最左边点叉叉删除
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        ResponseData responseData = new ResponseData();
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        if (!deleteCartItemResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            log.info("删除用户购物车失败,没有对应{}的商品", pid);
            return new ResponseUtil<>().setErrorMsg("删除失败");
        }
        log.info("删除用户购物车成功,删除商品名为{}", pid);
        return new ResponseUtil<>().setData("成功");
    }


    //删除购物车选择的商品
    @DeleteMapping("/shopping/items/{id}")
    public ResponseData deleteCartItemBySelect(@PathVariable("id") Long uid) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            log.info("删除失败");
            return new ResponseUtil<>().setErrorMsg("请核实需要删除的用户");
        }
        //日志已经在所删除部分
        return new ResponseUtil<>().setData("成功");
    }


    @RequestMapping("/shopping/items")
    public ResponseData selectAllOrNo(@RequestBody Map param) {
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        if ((Boolean) param.get("checked")) {
            checkAllItemRequest.setChecked("true");
        } else {
            checkAllItemRequest.setChecked("false");
        }
        checkAllItemRequest.setUserId(Long.parseLong((String) param.get("userId")));
        CheckAllItemResponse checkAllItemResponse = iCartService.checkAllCartItem(checkAllItemRequest);
        if (!checkAllItemResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            log.info("全选或者反选失败请检查参数");
            return new ResponseUtil<>().setErrorMsg("失败");
        }
        log.info("全选成功参数信息{}", param);
        return new ResponseUtil<>().setData("成功");
    }
}
